using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;

namespace org.jawk.util
{
	/// <summary>
	/// Load classes from a particular directory, disregarding the
	/// environmental classpath setting.
	/// </summary>
	/// <remarks>
	/// Load classes from a particular directory, disregarding the
	/// environmental classpath setting.
	/// This is useful when a directory is specified for class files,
	/// and it would not make sense to deviate from that directory.
	/// <p>
	/// For Jawk, this is used when the -d argument is present.
	/// </remarks>
	public sealed class DestDirClassLoader //: java.lang.ClassLoader
	{

        List<Assembly> _assemblyList = new List<Assembly>();

		public DestDirClassLoader(string dirname)
		{
		    foreach (string f in Directory.GetFiles(dirname,"*.dll"))
		    {
                try
                {
                    System.Reflection.Assembly a = System.Reflection.Assembly.LoadFrom(f);
                    _assemblyList.Add(a);
                }
                catch(Exception ex)
                {
                    Console.Error.WriteLine(ex.ToString());
                }
		    }

		}

        public Type LoadClass(string name)
        {
            Type t = null;
            foreach (Assembly a in _assemblyList)
            {
                t = a.GetType(name, false);
                if (t != null)
                {
                    break;
                }
            }
            return t;
        }

		
	}
}
